//每个线程循环 1w 次，累加变量 count 的值，count 默认值为 0，
// 注意线程安全问题。
class counter {
    public static int count;
    synchronized public void add() {
        count++;
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        counter count = new counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                count.add();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                count.add();
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(counter.count);
    }
}
